package server;

import org.ansj.splitWord.analysis.NlpAnalysis;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.*;

/**
 * Created by xjzhang on 2017/12/5.
 */
public class OutTermProcesser {



    Directory directory = new RAMDirectory();

    Similarity similarity = new BM25Similarity();

    Analyzer hanalyzer = new ANSJBinaryAnalyzer(new NlpAnalysis());

    IndexSearcher iSearcher;



    public void index(String indexContentPath) throws Exception{
        //directory = FSDirectory.open(Paths.get("aa"));

        try {
            //建立索引
            IndexWriterConfig config = new IndexWriterConfig(hanalyzer);
            config.setSimilarity(similarity);
            IndexWriter iWriter = new IndexWriter(directory, config);
            String line = "";
            File file = new File(indexContentPath);
            InputStreamReader inputStreamReader= new InputStreamReader(new FileInputStream(file),"utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            while ((line = bufferedReader.readLine()) != null){
                String command = line.split("\t")[0];
                String content = line.split("\t")[1];
                Document document = new Document();
                document.add(new Field("command",command,TextField.TYPE_STORED));
                document.add(new Field("content",content,TextField.TYPE_STORED));
                iWriter.addDocument(document);
            }
            iWriter.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String search(String queryStr,float thread){

        String ret ="";
        try {
            DirectoryReader iReader = DirectoryReader.open(directory);
            iSearcher = new IndexSearcher(iReader);
            iSearcher.setSimilarity(similarity);  //与上边配套的设置
            //queryStr= QueryParser.escape(queryStr);  //进行转义处理！！！
            QueryParser parser = new QueryParser("content", hanalyzer);
            Query query = parser.parse(queryStr);
            ScoreDoc[] hits = iSearcher.search(query, null, 1000).scoreDocs;
            if (hits.length==0 || hits[0].score<thread) {
                return "";
            } else {
                    Document hitDoc = iSearcher.doc(hits[0].doc);
                    ret = hitDoc.get("command");
                }
/*            iReader.close();
            directory.close();*/
        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }



}
